package cn.edu.zju.oms.container;

import org.springframework.stereotype.Component;
import tech.powerjob.common.utils.NetUtils;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.TaskResult;
import tech.powerjob.worker.core.processor.sdk.BroadcastProcessor;
import tech.powerjob.worker.log.OmsLogger;

import java.util.List;

/**
 * ContainerBroadcastProcessor
 *
 * @author tjq
 * @since 2020/5/17
 */
@Component
public class ContainerBroadcastProcessor implements BroadcastProcessor {

    @Override
    public ProcessResult preProcess(TaskContext context) throws Exception {
        context.getOmsLogger().info("start to preProcess, current worker IP is {}.", NetUtils.getLocalHost());
        return new ProcessResult(true, "preProcess successfully");
    }

    @Override
    public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) throws Exception {
        OmsLogger omsLogger = context.getOmsLogger();

        omsLogger.info("start to postProcess, current worker IP is {}.", NetUtils.getLocalHost());

        omsLogger.info("====== All Node's Process Result ======");
        taskResults.forEach(r -> omsLogger.info("taskId:{},success:{},result:{}", r.getTaskId(), r.isSuccess(), r.getResult()));
        return new ProcessResult(true, "postProcess successfully!");
    }

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {
        taskContext.getOmsLogger().info("start to process, current worker IP is {}.", NetUtils.getLocalHost());
        return new ProcessResult(true, NetUtils.getLocalHost() + " process successfully!");
    }
}
